﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormStuffInfo : FormTemplet
    {
        private StuffCondition _stuffCondition = null;

        private DataTable _dtStuffs = null;

        public FormStuffInfo()
        {
            this.InitializeComponent();
        }

        public FormStuffInfo(StuffCondition sc) : this()
        {
            this._stuffCondition = sc;
            this.LoadStuffsByCondition();
        }

        private void LoadStuffsByCondition()
        {
            Stuffs stuffs = new Stuffs();
            try
            {
                this._dtStuffs = stuffs.GetStuffsByCondition(this._stuffCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            stuffs.Dispose();
            if (this._dtStuffs != null)
            {
                if (this._dtStuffs.Rows.Count > 0)
                {
                    this.dgStuff.DataSource = null;
                    this.dgStuff.DataSource = this._dtStuffs;
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgStuff.DataSource = null;
                }
            }
        }

        private void FormStuffInfo_Load(object sender, EventArgs e)
        {
            this.dgStuff.AutoResizeColumns();
        }

        private void btnShowTotal_Click(object sender, EventArgs e)
        {
            if (this._dtStuffs != null)
            {
                if (this._dtStuffs.Rows.Count < 1)
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    int num = 0;
                    double num2 = 0.0;
                    for (int i = 0; i < this._dtStuffs.Rows.Count; i++)
                    {
                        num += Convert.ToInt32(this._dtStuffs.Rows[i][4]);
                        num2 += Convert.ToDouble(this._dtStuffs.Rows[i][5]);
                    }
                    this.txtResult.Text = "总数量：" + num.ToString() + "  总面积：" + num2.ToString();
                }
            }
        }

        private void btnShowTypeSubtotal_Click(object sender, EventArgs e)
        {
            Stuffs stuffs = new Stuffs();
            DataTable dataTable = null;
            try
            {
                dataTable = stuffs.GetTypeSubtotalByCondition(this._stuffCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            stuffs.Dispose();
            if (dataTable != null)
            {
                string text = "※ 分类信息如下：\r\n";
                int num = 0;
                double num2 = 0.0;
                string text2;
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    num += Convert.ToInt32(dataTable.Rows[i][1]);
                    num2 += Convert.ToDouble(dataTable.Rows[i][2]);
                    text2 = text;
                    text = string.Concat(new string[]
                    {
                        text2,
                        "\r\n",
                        (i + 1).ToString().PadRight(3, ' '),
                        "、类型：",
                        dataTable.Rows[i][0].ToString().PadRight(16, ' '),
                        "数量：",
                        dataTable.Rows[i][1].ToString().PadRight(16, ' '),
                        "面积：",
                        dataTable.Rows[i][2].ToString().PadRight(16, ' ')
                    });
                }
                text2 = text;
                text = string.Concat(new string[]
                {
                    text2,
                    "\r\n\r\n     ★数量为：",
                    num.ToString(),
                    "；   总平方为：",
                    num2.ToString()
                });
                MessageBox.Show(text, "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("可能没有数据信息！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            if (dataTable != null)
            {
                dataTable.Dispose();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgStuff.Rows[0].Cells[0].Value.ToString();
            }
            catch
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgStuff.CurrentCell.RowIndex;
            FormUpdateStuff formUpdateStuff = new FormUpdateStuff(Convert.ToInt32(this.dgStuff.Rows[rowIndex].Cells[0].Value));
            formUpdateStuff.MdiParent = base.MdiParent;
            formUpdateStuff.Show();
            formUpdateStuff.Activate();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
